package nz.co.doherty.gwtpbreadcrumbs.client.ui.common;

import nz.co.doherty.gwtpbreadcrumbs.client.i8n.ClientConstants;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.inject.Provider;

abstract public class ListViewImpl<Entity_, ListViewPresenter_ extends ListView.Presenter> extends
		Composite implements ListView<Entity_> {

	private Provider<ListViewPresenter_> presenterProvider;

	@SuppressWarnings("unchecked")
	@UiTemplate("ListView.ui.xml")
	interface Binder extends UiBinder<Widget, ListViewImpl> {
	}

	private static Binder uiBinder = GWT.create(Binder.class);

	@UiField
	FlowPanel buttons;

	@UiField
	FlowPanel itemList;
	
	protected ClientConstants constants;

	@Inject
	public ListViewImpl(Provider<ListViewPresenter_> presenterProvider, ClientConstants constants) {
		initWidget(uiBinder.createAndBindUi(this));
		this.presenterProvider = presenterProvider;
		this.constants = constants;

		for (Button button : getButtons()) {
			this.buttons.add(button);
		}
	}

	public Widget asWidget() {
		return this;
	}

	@Override
	public void addContent(Object slot, Widget content) {
		assert content != null : "Cannot add null content.";
		if (slot == ListPresenter.TYPE_ListContent) {
			itemList.add(content);
		} else {

		}
	}

	@Override
	public void setContent(Object slot, Widget content) {
		itemList.clear();
		if (content != null) {
			if (slot == ListPresenter.TYPE_ListContent) {
				addContent(slot, content);
			} else {

			}
		}
	}

	abstract public Button[] getButtons();

	protected ListViewPresenter_ getPresenter() {
		return this.presenterProvider.get();
	}

}
